//
//  RBGoodsOrderStarView.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/15.
//

#import "RBGoodsOrderStarView.h"
#import <UIImage+YYAdd.h>
#import "RanTool.h"
#import <Masonry.h>

@interface RBGoodsOrderStarView()

@property(nonatomic, strong)UILabel *textL;

@end

@implementation RBGoodsOrderStarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.textL = UILabel.new;
        [self addSubview:self.textL];
        [self.textL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        self.star = 3;
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGFloat x = [touch locationInView:self].x;
    self.star = x/([self itemW] + [self padding]) + 1;
}

- (CGFloat)padding
{
    return Zoom(17);
}

- (CGFloat)itemW
{
    return Zoom(18);
}

- (CGSize)suggestSize
{
    return CGSizeMake([self padding]*4 + [self itemW]*5 + 2, [self itemW] + 2);
}

- (void)setStar:(NSInteger)star
{
    _star = star;
    CGFloat padding = [self padding];
    CGFloat itemW = [self itemW];
    UIImage *selImg = [UIImage imageWithColor:UIColor.redColor];
    UIImage *norImg = [UIImage imageWithColor:UIColor.lightGrayColor];
    UIImage *paddingImg = [UIImage imageWithColor:UIColor.clearColor];
    self.textL.attributedText = [RanTool attributeStringMaker:^(NSMutableArray *maker) {
        for (NSInteger i = 1; i <= 5; i++)
        {
            if (i != 1)
            {
                [maker addAttributeStringWithImage:paddingImg imageFrame:CGRectMake(0, 0, padding, itemW)];
            }
            [maker addAttributeStringWithImage:star>=i? selImg: norImg imageFrame:CGRectMake(0, 0, itemW, itemW)];
        }
    }];
}

@end
